# Maintainer: Taner Tas <taner76@gmail.com>
pkgname=mame
pkgver=0.245
_pkgver=${pkgver/.}
_groovymame=gm0245sr002k
pkgrel=0
pkgdesc="Multi Arcade Machine Emulator with GroovyMAME/Switchres/No-nag patchset."
url="https://mamedev.org/"
# riscv64: takes forever to build and constantly fails
arch="all !riscv64"
license="GPL-2.0-or-later"
depends="$pkgname-common"
makedepends="
	alsa-lib-dev
	coreutils
	expat-dev
	flac-dev
	fontconfig-dev
	glm-dev
	libjpeg-turbo-dev
	libxi-dev
	libxinerama-dev
	libxrandr-dev
	lua5.3-dev
	mesa-dev
	nasm
	portaudio-dev
	portmidi-dev
	pulseaudio-dev
	python3-dev
	py3-sphinx
	rapidjson-dev
	sdl2-dev
	sdl2_ttf-dev
	sqlite-dev
	zlib-dev
	"
subpackages="
	$pkgname-common::noarch
	$pkgname-arcade
	$pkgname-mess
	$pkgname-data::noarch
	$pkgname-doc
	$pkgname-lang
	$pkgname-tools
	$pkgname-plugins::noarch
	"
source="
	https://github.com/mamedev/mame/archive/mame$_pkgver.tar.gz
	$_groovymame.patch::https://github.com/antonioginer/GroovyMAME/releases/download/$_groovymame/GroovyMAME${pkgver#*.}.patch
	mame.ini
	midi.conf
	fix-musl.patch
	nonag.patch
	"
options="!check" # No test suite
builddir="$srcdir"/$pkgname-$pkgname${pkgver/.}

# temp allow textrels on rv64
case $CARCH in
	riscv64) options="$options textrels" ;;
esac

build() {
	case "$CARCH" in
		x86|armhf|armv7) _PTR64=0;;
		*) _PTR64=1;;
	esac

	case "$CARCH" in
		x86_64|x86) _NOASM=0 ;;
		*) _NOASM=1 ;;
	esac

	case "$CARCH" in
		s390x) _BIGENDIAN=1 ;;
		*) _BIGENDIAN=0 ;;
	esac

	export CFLAGS="$CFLAGS -I/usr/include/lua5.3 -DBX_CRT_MUSL -Wno-everything"
	export CXXFLAGS="$CXXFLAGS $CFLAGS"
	export LDFLAGS="$LDFLAGS -L/usr/lib/lua5.3"

_build="make
	TARGETOS=linux
	NOWERROR=1
	DEBUG=0
	USE_QTDEBUG=0
	VERBOSE=1
	REGENIE=1
	OSD=sdl
	SYMBOLS=0
	FORCE_DRC_C_BACKEND=
	PTR64=$_PTR64
	NOASM=$_NOASM
	BIGENDIAN=$_BIGENDIAN
	TOOLS=
	PYTHON_EXECUTABLE=python3
	SDL_INI_PATH=/etc/mame
	USE_SYSTEM_LIB_EXPAT=1
	USE_SYSTEM_LIB_ZLIB=1
	USE_SYSTEM_LIB_JPEG=1
	USE_SYSTEM_LIB_FLAC=1
	USE_SYSTEM_LIB_LUA=1
	USE_SYSTEM_LIB_SQLITE3=1
	USE_SYSTEM_LIB_PORTAUDIO=1
	USE_SYSTEM_LIB_UV=1
	USE_SYSTEM_LIB_GLM=1
	USE_SYSTEM_LIB_RAPIDJSON=1
	USE_SYSTEM_LIB_PORTMIDI=1
	USE_SYSTEM_LIB_ASIO=0"

	[ -f mame ] || [ -f mame64 ] || $_build TOOLS=1 SUBTARGET=mame
	[ -f mamearcade ] || [ -f mamearcade64 ] || $_build SUBTARGET=arcade
	[ -f mess ] || [ -f mess64 ] || $_build SUBTARGET=mess
}

package() {
	install -D -m755 mame "$pkgdir"/usr/bin/mame || \
	install -D -m755 mame64 "$pkgdir"/usr/bin/mame
	install -D -m644  docs/man/mame.6  "$pkgdir"/usr/share/man/man6/mame.6
	for i in castool.1 \
		chdman.1 \
		floptool.1 \
		imgtool.1 \
		jedutil.1 \
		ldresample.1 \
		ldverify.1 \
		romcmp.1
	do
		install -D -m644 docs/man/"$i"  "$pkgdir"/usr/share/man/man1/"$i"
	done
}

common() {
	cd "$builddir"
	pkgdesc="MAME - Common configuration files"
	install -D -m644 "$srcdir"/mame.ini "$subpkgdir"/etc/mame/mame.ini
	install -D -m644 "$srcdir"/midi.conf "$subpkgdir"/usr/lib/modules-load.d/midi.conf
	install -d -m755 "$subpkgdir"/usr/share/$pkgname/roms
}

arcade() {
	pkgdesc="Multi Arcade Machine Emulator - Arcade specific build."
	cd "$builddir"
	install -D -m755 mamearcade "$subpkgdir"/usr/bin/mamearcade || \
	install -D -m755 mamearcade64 "$subpkgdir"/usr/bin/mamearcade
	ln -s mamearcade "$subpkgdir"/usr/bin/arcade
}

mess() {
	pkgdesc="Multi Arcade Machine Emulator - Mess specific build."
	cd "$builddir"
	install -D -m755 mamemess "$subpkgdir"/usr/bin/mess || \
	install -D -m755 mamemess64 "$subpkgdir"/usr/bin/mess
}

data() {
	pkgdesc="Distribution data files for MAME"
	cd "$builddir"
	mkdir -p "$subpkgdir"/usr/share/$pkgname
	for i in artwork \
		bgfx \
		hash \
		hlsl \
		ini \
		keymaps \
		samples
	do
		cp -r "$i" "$subpkgdir"/usr/share/$pkgname
	done
	rm -rf "$subpkgdir"/usr/share/$pkgname/bgfx/shaders/dx11
	rm -rf "$subpkgdir"/usr/share/$pkgname/bgfx/shaders/dx9
	rm -rf "$subpkgdir"/usr/share/$pkgname/bgfx/shaders/metal
}

plugins() {
	pkgdesc="Distribution plugins for MAME"
	cd "$builddir"
	mkdir -p "$subpkgdir"/usr/share/$pkgname
	cp -r plugins "$subpkgdir"/usr/share/$pkgname/
}

tools() {
	pkgdesc="Tools for MAME"
	cd "$builddir"
	mkdir -p "$subpkgdir"/usr/bin
	for i in castool \
		chdman \
		floptool \
		imgtool \
		jedutil \
		ldresample \
		ldverify \
		romcmp
	do
		install -sm755 "$i" "$subpkgdir"/usr/bin
	done
}

lang() {
	pkgdesc="Localization files for MAME"
	cd "$builddir"
	mkdir -p "$subpkgdir"/usr/share/$pkgname
	cp -r language "$subpkgdir"/usr/share/$pkgname/
}
sha512sums="
ebac675d5df6dc5b04ef4847732f58e2b4c32b66bb698d8e245a5b14e6b36838fdce26045a3a80cd34885544bf59a665446b4a4bb202733848dcc115aecdea49  mame0245.tar.gz
d761ed827965fd08e03865901b461b64e453431b4d1fb5bdfcb07e23344807423c254e8e234537197439eba042cf70c24934d5f566ce788c3b72a03b4ba93f25  gm0245sr002k.patch
dc008245cbea0b94f58d83e09bf5fd3fff04ac0e2f3a36b910a8b7633c5377419fc67a1fd366ef268e283f744d9a8d29928cfacf456b3edaa2d0b1a11d46a701  mame.ini
8f83ff5a916f4ff8e86c5afbdfe4475f7780bb36c20c78d6d029d0eb0dafd77b3471faa538aca384001d2049dc94c4df3429c67d743adde9fd6329c91e6d19a2  midi.conf
32fa59744e426a0921e44e1e792dbdc263cf8e505407adfe1dc392aa949db96bf8fce2140a3f5ede5a5ba756cf40d80887c502f52cec805c34c477ac44c21049  fix-musl.patch
3e4e1ccfc506d18cd4d78ea477b53bf9854568c1eab64e6dc63bbdc750dcd94a2229635d17dbe9c5ee75b28baffeac5c87850f7c87b0dd400636811a961d8d9d  nonag.patch
"
